# ----------------------------------------------------------------------
# Jadoon and Lucas
# Winning or Losing Hearts and Minds? (JoP)
# [Figures script]
# ----------------------------------------------------------------------

# start txt output
sink("JadoonLucasAFG_figures.txt")

library(tidyverse)
library(stargazer)
library(ggpubr)

# ----------------------------------------------------------------------
# Fig 1
# ----------------------------------------------------------------------

# fig1.xlsx is created by do_analysis.do - please run that first
input <- readxl::read_xlsx("fig1.xlsx"
                           ,col_names = c("no", "yes")
                           )

dichotomous <- tibble(pred    = c(input$no[1], input$yes[1])
                      ,status = c("No", "Yes")
                      ,low    = c(input$no[5], input$yes[5])
                      ,high   = c(input$no[6], input$yes[6])
                      )

p <- ggplot() + 
  geom_point(data    = dichotomous
             ,aes(x  = status 
                  ,y = pred)
             ,size   = 4
             ,color  = "black") + 
  geom_segment(aes(x     = status
                   ,y    = low
                   ,xend = status
                   ,yend = high)
               ,color    = "black"
               ,size     = 1
               ,data     = dichotomous) +
  xlab('Recent Leadership Decapitation') + 
  ylab('Pr(Govt Securing Country Well)') +
  ggpubr::theme_pubr() +
  theme(plot.title   = element_text(hjust = 0.5)
        ,text        = element_text(size  = 12)
        ,axis.text.x = element_text(size  = rel(1.2), angle = 0)
        ,axis.text.y = element_text(size  = rel(1.3), angle = 0)
  ) +
  ylim(.28, .42)
p

# ----------------------------------------------------------------------
# Appendix: Fig 1a
# ----------------------------------------------------------------------

load("appendix.rda")

syr     <- sum(as.numeric(str_detect(paj_clean$text_clean, "syria|iraq"))) / nrow(paj_clean)
afg     <- sum(as.numeric(str_detect(paj_clean$text_clean, prov_matches))) / nrow(paj_clean)
afg_all <- sum(as.numeric(str_detect(paj_clean$text_clean, paste(c(prov_matches,dist_matches,"afghan"), collapse = "|")))) / nrow(paj_clean)

paj <- tibble(proportion = c(syr, afg, afg_all)
              ,terms     = c("Syria or Iraq"
                             ,"Afghan* or Provinces"
                             ,"Afghan*, Provinces,\nor Districts")
              )

p <- ggdotchart(paj
                ,x          = "terms"
                ,y          = "proportion"
                ,sorting    = "descending"
                ,palette    = c('#00BFC4', '#F8766D')
                ,add        = "segments"        
                ,title      = "Keyword Mentions in Pajhwok Articles with 'Da'esh'"
                ,ylab       = "Proportion of Articles"
                ,xlab       = ""
                ,rotate     = TRUE
                ,color      = "#F8766D"
                ,dot.size   = 6
                ,add.params = list(color  = "lightgray"
                                  ,size    = 1.5)
                ) +
  ggpubr::theme_pubr(base_family          = "Garamond") +
  theme(plot.title   = element_text(hjust = 0.5)
        ,text        = element_text(size  = 15)
        ,axis.text.x = element_text(size  = rel(1.2), angle = 0)
        ,axis.text.y = element_text(size  = rel(1.3), angle = 0)
  ) +
  scale_y_continuous(limits  = c(0,1)
                     ,expand = c(0, 0)
                     ) +
  theme(panel.grid.major  = element_blank()
        ,panel.grid.minor = element_blank()
        )
p

# ----------------------------------------------------------------------
# Descriptive Stats (appendix A7)
# ----------------------------------------------------------------------

x_sum <- haven::read_dta("dat.dta") %>%
  select(Q481fd,Q190fd,Q177fd,any_isk_kill30,any_isk_kill_nato30
         ,any_isk_kill_afg30,n_isk_killed30,n_isk_killed_afg30,n_isk_killed_nato30
         ,Income_Int,Age,nattacks_lag30,Gender,Ethnic_Cat,Educ_Cat) %>%
  drop_na(Q177fd,Ethnic_Cat,Educ_Cat,Income_Int,Age,Gender) %>%
  # categorical vars
  select(-c(Ethnic_Cat,Educ_Cat))

stargazer::stargazer(as.data.frame(x_sum))

# end txt output
sink()

# ----------------------------------------------------------------------
# //end
# ----------------------------------------------------------------------



